$(function () {
	$('.share-btn').bind('click', function () {
		$('.box').css('display', 'block');
	});
	$('.close').bind('click', function () {
		$('.box').css('display', 'none');
	});
		var count = 0;
	$('.da-car-add').on('click', function(event) {
		var
			offset = $('.side-cars').offset(),
		 	img = $('.goods-big').find('img').attr('src'),
		 	flyer = $('<img class="u-flyer" src="' + img + '">');
		 	console.log(event.pageX);
		flyer.fly({
			start: {
				left: event.pageX,
				top: event.pageY
			},
			end: {
				left: offset.left + 10,
				top: offset.top + 20,
				width: 20,
				height: 20
			},
			onEnd: function() {
//				$("#msg").show().animate({ width: '250px' }, 200, function() {
//					$("#msg").hide();
//				});
				this.destory();
			}
			
		});
		count++;
		console.log(count);
		$('.da-car-count').html(count);
		$('.car-number').html(count);
		
		//添加到cookie
		// 第一次添加商品
			var
				isAdd = false,  // 假设没有商品没有添加过
				sGoods = cookie.getCookie('goods');

			if(typeof sGoods === 'undefined') {
				var aGoods = []; // 相当于购物的车子
			} else {
				var aGoods = JSON.parse(sGoods);

				// 判断当前商品有没有添加过
				for(var i = 0; i < aGoods.length; i++) {
					if(aGoods[i].id == $(this).data('id')) {
						aGoods[i].num++;
						isAdd = true;
						break;
					}
				}
			}
			// 如果isAdd为false,说明商品没有添加过
			if(!isAdd) {
				var oGoods = {
					id:    $(this).data('id'),
					title: $(this).data('title'),
					price: $(this).data('price'),
					url:   $(this).data('url'),
					num:   1
				};

				aGoods.push(oGoods);
			}

			// 存储cookie
			cookie.setCookie('goods', JSON.stringify(aGoods), 7);
	});
	//款式部分，点击小图片，然后对应的大图片改变
	$('.goods-small-img a').on('click', function () {
		var smallImg = $(this).find('img').attr('src');
		$('.goods-big img').attr('src', smallImg);
	});
	//购买数量部分
		var iNumber = 1;
	$('.ico-add').on('click', function () {
		iNumber++;
		$('.number-input').attr('value', iNumber);
		console.log($('.number-input').val());
	});
	
	$('.ico-reduce').on('click', function () {
		
		if (iNumber > 1) {
		iNumber--;
		console.log(iNumber);
		$('.number-input').attr('value', iNumber);
			
		} else {
			alert('最小数量只能为1！！！！！');
		}
	});
	//侧边购物车
	$('.side-cars').on('mouseover', function () {
		$('.side-cars').css('background', '#E14958');
	});
	$('.side-cars').on('mouseleave', function () {
		$('.side-cars').css('background', '#333333');
	});
	var si = 1;
	$('.side-cars').on('click', function (){
		si++;
		if (si % 2 === 0) {
			$('.da-sidebar').animate({width:300}, 200);
		} else {
			$('.da-sidebar').animate({'width':300}, 200,function (){
				$('.sidebar-functoin').css('background', '#333333');
			}).css('background', '#EEEEEE').animate({width:40},200);
		}
	});
	//回到最上边
	$('.goTop').on('click', function () {
		$("html,body").animate({scrollTop:0}, 500);
	});
});